//var services = "http://120.78.71.178:7717"
// var services = "http://120.78.71.178:7707"
var services = "http://zph.winzkj.com/api/i";
var companyServices = "http://zph.winzkj.com/api/c";
//倒计时
var countDown=60;
function setTime(val) {
    if (countDown == 0) {
        val.removeAttribute("disabled");
        val.value="获取验证码";
        countDown = 60;
        return false;
    } else {
        val.setAttribute("disabled", true);
        val.value="重新发送(" + countDown + ")";
        countDown--;
    }
    setTimeout(function() {
        setTime(val);
    },1000);
}

//调用layUI
function useLayUI() {
    //时间插件
    layui.use('laydate', function(){
        var laydate = layui.laydate;
        //同时绑定多个
        lay('.date-plug').each(function(){
            laydate.render({
                elem: this
                ,trigger: 'click'
            });
        });
    });
    //下拉列表
    layui.use('form', function(){
        var form = layui.form;
        //局部渲染
        form.render('select','select');
    });
    //tab切换
    layui.use('element', function(){
        var element = layui.element;
    });
    //分页
    // layui.use('laypage', function(){
        // var laypage = layui.laypage;
        // //执行一个laypage实例
        // laypage.render({
        //     elem: 'succ_paging' ,//注意，这里的 test1 是 ID，不用加 # 号
        //     count: 20 ,//数据总数，从服务端得到
        //     limit:6
        // });
        // laypage.render({
        //     elem: 'search_paging' ,
        //     count: 20 ,
        //     limit:10
        // });
    //     laypage.render({
    //         elem: 'accepted' ,
    //         count: 20 ,
    //         limit:9
    //     });
    //     laypage.render({
    //         elem: 'not_view' ,
    //         count: 20 ,
    //         limit:9
    //     });
    //     laypage.render({
    //         elem: 'hr_manage' ,
    //         count: 20 ,
    //         limit:9
    //     });
    // });
}

//layUI弹窗
function popupDiv(obj){
    //弹窗插件
    layui.use('layer', function() {
        var $ = layui.jquery, layer = layui.layer;
        //触发事件
        layer.open({
            type: 1,
            content: $(obj) ,
            area: '432px',
            offset: '0',//36px
            shade: [0.5, '#000'],
            skin: 'pop-style',
            title: false,
            closeBtn:0,
            shadeClose:true,
            scrollbar: false
        });
    })
}

//动态添加工作经验
function addJobExper(obj) {
    var jobExper = '<ul class="content personal-info clone-el">\n' +
        '        <li>\n' +
        '            <span>公司名称</span><input type="text" name="company[]" placeholder="请输入公司名称"/>\n' +
        '        </li>\n' +
        '        <li  class="date wap-work-date">\n' +
        '            <span>工作时间</span><input type="text" name="entryTime[]" placeholder="日期" class="date-plug"/><i>至</i><input type="text" name="separationTime[]" placeholder="日期" class="date-plug"/>\n' +
        '        </li>\n' +
        '        <li>\n' +
        '            <span>工作部门</span><input type="text" name="department[]" placeholder="请输入部门名称"/>\n' +
        '        </li>\n' +
        '        <li class="layui-form itemselect" lay-filter="select">\n' +
        '            <span>职位选择</span><input type="text" name="position[]" placeholder="请输入职位 "/>\n' +
        '        </li>\n' +
        '        <li class="describe">\n' +
        '            <span>工作描述</span><textarea name="workContext[]" placeholder="可以描述您的职责范围、工作任务以及获得的成绩等"></textarea>\n' +
        '        </li>\n' +
        '    </ul>';
    $(jobExper).insertBefore(obj);
}

//动态添加专业技能（弹窗）
function addSkill(obj) {
    var skill = '<div class="skill-list">\n' +
        '            <div class="section">\n' +
        '                <span>技能名称</span><input type="text" name="" placeholder="请输入技能名称"/>\n' +
        '            </div>\n' +
        '            <div class="section">\n' +
        '                <span>熟悉程度</span><div class="layui-form itemselect" lay-filter="select">\n' +
        '                <div class="layui-form-item">\n' +
        '                    <select name="">\n' +
        '                        <option value="">请选择</option>\n' +
        '                        <option value="">精通</option>\n' +
        '                        <option value="">熟练</option>\n' +
        '                        <option value="">了解</option>\n' +
        '                    </select>\n' +
        '                </div></div>\n' +
        '            </div>\n' +
        '        </div>';
    $(skill).insertBefore(obj);
}

// //动态添加专业技能（注册）
// function regiAddSkill(obj) { 
//     var skill = '<ul>\n' +
//         '                <li>\n' +
//         '                    <span>技能名称</span><input type="text" name="skillName[]" placeholder="请输入技能名称"/>\n' +
//         '                </li>\n' +
//         '                <li class="layui-form itemselect" lay-filter="select" >\n' +
//         '                    <span>熟悉程度</span>\n' +
//         '                    <div class="layui-form-item" >\n' +
//         '                        <select name="proficiency[]"  id="proficiency" >\n' +
//         '                            <option value="">请选择</option>\n' +
//         '                            <option value="">熟练</option>\n' +
//         '                            <option value="">精通</option>\n' +
//         '                            <option value="">了解</option>\n' +
//         '                        </select>\n' +
//         '                    </div>\n' +
//         '                </li>\n' +
//         '            </ul>';
//     $(skill).insertBefore(obj);
// }

//动态添加项目经验（注册）
function regiAddProExper(obj) {
    var proExper = ' <ul>\n' +
        '                <li>\n' +
        '                    <span>项目名称</span><input type="text" name="projectName[]" placeholder="请输入项目名称"/>\n' +
        '                </li>\n' +
        '                <li>\n' +
        '                    <span>项目职责</span><input type="text" name="duties[]" placeholder="请输入项目职责"/>\n' +
        '                </li>\n' +
        '                <li class="describe">\n' +
        '                    <span>完成内容</span><textarea  name="workContext[]" placeholder="可以描述您的职责范围、工作任务以及获得 的成绩等"></textarea>\n' +
        '                </li>\n' +
        '            </ul>';
    $(proExper).insertBefore(obj);
}

//上传头像
function handleFiles(obj,id) {
    file = document.getElementById(id);
    var files = obj.files;
    img = new Image();
    if(window.URL){
        //File API
        img.src = window.URL.createObjectURL(files[0]); //创建一个object URL，并不是你的本地路径
        img.onload = function(e) {
            window.URL.revokeObjectURL(this.src); //图片加载后，释放object URL
        }
    }
    file.src=img.src;
    //上传文件
    var fd = new FormData(),//实例化一个表单
        xhr = new XMLHttpRequest();
    fd.append('headimg', files[0]);//追加图片元素
    xhr.open('post', 'user.php?act=act_edit_img');//请求方式，请求地址
    xhr.send(fd);
}

//IE8
$(function(){
    if($.browser.msie && parseInt($.browser.version) == 8) {
        $('.resume-manage-page .right-part .card-list > li:nth-child(3n+3)').css('margin-right','0');
    }
})

//弹出层
function layerMsg(str){
    layui.use('layer', function(){
        layer.ready(function(){
            layer.msg(str);
            });
    });
}
//判断手机号码格式
function myregtel(str){
    var myreg =/^[1][0-9]{10}$/;
    if (!myreg.test(str)) {
        return false;
    } else {
        return true;
    }
}
//校验手机号
function telchek(tel){
    if(tel==""||tel==null){
        console.log(tel)
        layerMsg("手机号码不为空")
        return false;
    }
    else if(myregtel(tel)==false){
        layerMsg("手机号码格式错误")
        return false;
    }else{
        return true;
    }
}
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
function codeReset(obj){
    var tel = $('#tel').val();
    if(telchek(tel)==false){
        return false;
    }
    setTime(obj)
    var url = '';
    var id = getUrlParam('id');
    if(id==1){
        url = services+'/user/vcode/reset/'+tel;
    }else{
        url = companyServices+'/user/vcode/reset/'+tel;
    }

    // if (isCompany == '1') {
    //     url = companyServices+'/user/vcode/reset/'+tel;
    // } else {
    //     url = services+'/user/vcode/reset/'+tel;
    // }


    $.ajax({
        type: 'get', // 提交方式 get/post
        url:url,// 需要提交的 url
        contentType:"application/json;charset=utf-8",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function(res) {
            if(res.status=="00200"){
                layerMsg(res.message)
            }else{
                layerMsg("获取验证码失败!请重试")
            }
        },
    })
}


 //获取验证码
function code(obj){

    var isCompany = $.cookie("isCompany");

    var tel = $('#tel').val();
    if(telchek(tel)==false){
        return false;
    }
    setTime(obj)

    var url = '';
    if (isCompany == '1') {
        url = companyServices+'/user/vcode/reg/'+tel;
    } else {
        url = services+'/user/vcode/reg/'+tel;
    }

    $.ajax({
        type: 'get', // 提交方式 get/post
        url:url,// 需要提交的 url
        contentType:"application/json;charset=utf-8",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function(res) {
            if(res.status=="00200"){
                layerMsg(res.message)
            }else{
                layerMsg("获取验证码失败!请重试")
            }
        },
    })
}
//登录状态
function LoginStatus(){
    var userString = $.cookie('userInfo');
    if(userString==null||userString==""){
        layerMsg("您的登录状态已过期，请重新登录!")
        setTimeout(function() {
            window.location.href="/login.html" ;
        },3000);
        return false
    }else{
        var userInfo = JSON.parse(userString)
        return userInfo;
    }
}

function checkLoginStatus(res) {
    if (res != null && res.status != null && res.status == '20011') {
        layerMsg("您的登录状态已过期，请重新登录!")
        setTimeout(function() {
            window.location.href="/login.html" ;
        },2000);
        return false
    }
}


//下拉列表ajax
function SelectAjax(_url,_type,_id,_name){
    _url = services+_url;
    $.ajax({
        type: _type, // 提交方式 get/post
        url:_url,
        async:false,
        // contentType:"application/json;charset=utf-8",
        //解决跨域
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function(res) {
            var obj = JSON.parse(res);
            if(obj.code==0){
                data = obj.data;
                for(var i=0;i<data.length;i++){
                    $("#"+_id).append("<option value='"+data[i].id+"'>"+data[i][_name]+"</option>");
                }
            }else{
                layerMsg("加载失败，请重试")
            }
        }
    })
}

//下拉列表搜索功能

function SelectSearchFrom(form,_id){
    // console.log(_id)
    form.on('select('+_id+')', function (data) {   //选择移交单位 赋值给input框
        $("#"+_id+"Text").val(data.elem[data.elem.selectedIndex].text);
        $('#'+_id+'Value').val(data.value)
        $('#'+_id+'Status').next().find("dl").css({ "display": "none" });
        form.render();
    });
}

//下拉列表渲染
function SelectUse(arr){
       
    layui.use('form', function(){
        var form = layui.form;
        form.render('select','select');
        //求职状态

        for(var i=0;i<arr.length;i++){
            SelectSearchFrom(form,arr[i])
        }

        window.search = function (obj) {
            //获取id
            var idText = obj['id'];
            console.log(idText)
            //截取前面id部分长度
            var idEnd = idText.indexOf("Text")
            //获取select id
            var id = idText.substr(0, idEnd)

            var value = $("#"+idText).val();
            $("#"+id).val(value);
            form.render();
            $("#"+id).next().find("dl").css({ "display": "block" });
            var dl = $("#"+id).next().find("dl").children();
            var j = -1;
            for (var i = 0; i < dl.length; i++) {
                if (dl[i].innerHTML.indexOf(value) <= -1) {
                    dl[i].style.display = "none";
                    j++;
                }
                if (j == dl.length-1) {
                    $("#"+id).next().find("dl").css({ "display": "none" });
                }
            }
        }

    });

}

//返回后台登录状态查看
function ApiLoginStatus(res){
    if(typeof(res)=="string"){
        result = JSON.parse(res);
    }else{
        result = res;
    }
    if(result.status==20011){
        layerMsg(result.message)
        $.cookie('userInfo', '', { expires: -1 });
        $.cookie('resumeId', '', { expires: -1 });
        var storage=window.localStorage;
        storage.clear();
        setTimeout(function() {
            window.location.href="/login.html" ;
        },3000);
        return  false;
    }
    return true;
}
function logout(){
    $.ajax({
        type:'post',
        async:false,
        url:services+"/user/logout",
        contentType:"application/json;charset=utf-8",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function(res) {
            var check = ApiLoginStatus(res)
            if(check==false)
                return false;
            console.log(res)
            layerMsg(res.message)
            $.cookie('userInfo', '', { expires: -1 });
            $.cookie('resumeId', '', { expires: -1 });
            $.cookie('my_intro','', { expires: -1 } );
            $.cookie('projects','', { expires: -1 } );
            $.cookie('skills','', { expires: -1 });
            $.cookie('education','', { expires: -1 });
            $.cookie('jobs','', { expires: -1 } );
            setTimeout(function() {
                window.location.href="/login.html" ;
            },3000);
        }
    })
}

function logoutCompany(){
    $.ajax({
        type:'post',
        async:false,
        url:companyServices+"/user/logout",
        contentType:"application/json;charset=utf-8",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function(res) {
            var check = ApiLoginStatus(res)
            if(check==false)
                return false;
            console.log(res)
            layerMsg(res.message)
            $.cookie('userInfo', '', { expires: -1 });
            setTimeout(function() {
                window.location.href="/login.html" ;
            },3000);

        }
    })
}

//获取参数
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}


function isMobile(){
    if(window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
        return true;  // 移动端
    }else{
        return false;  // PC端
    }
}

